package com.example.test;

import java.nio.ByteBuffer;
import java.util.ArrayList;

import sysu.ss.xu.FFmpeg;

import com.churnlabs.ffmpegsample.MainActivity;
import com.example.learnandroid.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.widget.ImageView;

public class ExtractVideo extends Activity{
	private ArrayList<Bitmap> lBitmap;
	private ImageView image;
	private FFmpeg ff;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_test_imageview);
		image = (ImageView) findViewById(R.id.test_imageview);
		//extractVideo();
		ShowImage si_th = new ShowImage();
		si_th.start();
		
		//ff = FFmpeg.getInstance2();
		
        
	}
	
	private void extractVideo(Thread th){
		try{
			long start = System.currentTimeMillis();
			final int width = 320;
			final int height = 240;
			
			// extract video to bitmap
			MainActivity.openFile("/sdcard/myvideo2.mp4");
			
			Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test0 );
			lBitmap = new ArrayList<Bitmap>();
			
			for(int i=0;i<100;i++){
				if(i%5==0){
					final Bitmap bm = mBitmap.copy(Bitmap.Config.ARGB_8888, true);
					//final Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
					MainActivity.drawFrame(bm ,width, height,1);
					
					ExtractVideo.this.runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							
							Bitmap bm2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
							int[] pixels = new int[width*height];
							bm.getPixels(pixels, 0, width, 0, 0, width, height);
							bm2.setPixels(pixels, 0, width, 0, 0, width, height);
							image.setImageBitmap(bm2);
							
						}
					});
					
					lBitmap.add(bm);
				}
				else{
					MainActivity.drawFrame(mBitmap ,width, height,0);
				}
				
			}
			
            
			
			long stop = System.currentTimeMillis();
			System.out.println(stop-start);
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	
	private void extractVideo2(Thread th){
		try{
			long start = System.currentTimeMillis();
			int width = 320;
			int height = 240;
			
			MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); 
			mediaMetadataRetriever.setDataSource("/sdcard/myvideo.mp4"); 
			for(int i=0;i<20;i++){
				
				final Bitmap bm = mediaMetadataRetriever.getFrameAtTime();
				
				ExtractVideo.this.runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						image.setImageBitmap(bm);
						
					}
				});
			}     
			
            
			
			long stop = System.currentTimeMillis();
			System.out.println(stop-start);
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	
	private void extractVideo3(Thread th){
		//FFmpeg2 ff2 = new FFmpeg2();
		ff = FFmpeg.getInstance();
		ff.openFile("/sdcard/myvideo.mp4");
		int width = ff.getWidth();
        int height = ff.getHeight();
        ff.prepareResources();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        byte[] nativePixels = ff.getNextDecodedFrame();
        ByteBuffer buffer = ByteBuffer.wrap(nativePixels);
        bitmap.copyPixelsFromBuffer(buffer);
        
        
	}
	
	private class ShowImage extends Thread{
		public void run(){
			extractVideo(this);
		}
	}
	
	private int dpToPx(int dp)
	{
	    float density = getApplicationContext().getResources().getDisplayMetrics().density;
	    return Math.round((float)dp * density);
	}
	
	private Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
		 
		int width = bm.getWidth();
		 
		int height = bm.getHeight();
		 
		float scaleWidth = ((float) newWidth) / width;
		 
		float scaleHeight = ((float) newHeight) / height;
		 
		// CREATE A MATRIX FOR THE MANIPULATION
		 
		Matrix matrix = new Matrix();
		 
		// RESIZE THE BIT MAP
		 
		matrix.postScale(scaleWidth, scaleHeight);
		 
		// RECREATE THE NEW BITMAP
		 
		Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
		 
		return resizedBitmap;
		 
	}
	
}
